﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using DealKhuyenMaiV2.com.Helpers;
namespace DealKhuyenMaiV2.com.Controllers
{

    public class HomeController : BaseController
    {


        [HttpGet]
        public ActionResult Index()
        {
           
            string Selected = "1";
            try
            {
                Selected = System.Web.HttpContext.Current.Request.Cookies["ChooseCity"].Value;

            }
            catch (NullReferenceException e)
            {
                Selected = "1";
            }
            int IDProvince = int.Parse(Selected);
            Global.Now = DateTime.UtcNow;
            List<Product> ListPro = new List<Product>();
             dynamic result;
                if (IDProvince != -1)
                {
                    result = from p in DbConText.Products where p.Province.IDProvince == IDProvince && p.StatusProduct.NameStatus == "Đã duyệt" select p;
                }
                else
                    result = from p in DbConText.Products where p.StatusProduct.NameStatus == "Đã duyệt" select p;
                foreach (Product p in result)
                    ListPro.Add(p);
          
            if(ListPro.Count > 0)
             ViewBag.Images = ListPro[0].Images.ToList();

            return View(ListPro);

        }
        [HttpPost]
        public ActionResult Index(FormCollection f)
        {
            List<Product> ListPro = new List<Product>();
            try
            {
                Global.Now = DateTime.UtcNow;
                int IDProvince = int.Parse(Request.Form["Options"]);
                Response.Cookies["ChooseCity"].Value = IDProvince.ToString();
                Response.Cookies["ChooseCity"].Expires = DateTime.Now.AddMinutes(30);
              
                dynamic result;
                if (IDProvince != -1)
                {
                    result = from p in DbConText.Products where p.Province.IDProvince == IDProvince && p.StatusProduct.NameStatus == "Đã duyệt" select p;
                }
                else
                    result = from p in DbConText.Products where p.StatusProduct.NameStatus == "Đã duyệt" select p;
                foreach (Product p in result)
                    ListPro.Add(p);
                ViewBag.Images = ListPro[0].Images.ToList();
                GetListProvince(IDProvince.ToString());
            }
            catch (Exception ex)
            {

            }
            return View(ListPro);
        }
        public ActionResult DealExpired()
        {
            string Selected = "1";
            try
            {
                Selected = System.Web.HttpContext.Current.Request.Cookies["ChooseCity"].Value;

            }
            catch (NullReferenceException e)
            {
                Selected = "1";
            }
            int IDProvince = int.Parse(Selected);
            Global.Now = DateTime.UtcNow;
            List<Product> ListPro = new List<Product>();
            dynamic result;
            if (IDProvince != -1)
            {
                result = from p in DbConText.Products where p.StatusProduct.NameStatus == "Hết hạn" && p.IDProvince == IDProvince select p;
            }
            else
            {
                result = from p in DbConText.Products where p.StatusProduct.NameStatus == "Hết hạn" select p;
            }
            foreach (Product p in result)
                ListPro.Add(p);

            return View(ListPro);
        }

        public ActionResult Search(string KeyWord)
        {
            string Selected = "1";
            try
            {
                Selected = System.Web.HttpContext.Current.Request.Cookies["ChooseCity"].Value;

            }
            catch (NullReferenceException e)
            {
                Selected = "1";
            }
            int IDProvince = int.Parse(Selected);
            List<Product> ListPro = new List<Product>();
            dynamic result;
            if (IDProvince != -1)
            {
                result = from p in DbConText.Products where p.NameProduct.IndexOf(KeyWord) > -1 && p.IDProvince == IDProvince &&
                         (p.StatusProduct.NameStatus == "Đã duyệt" || p.StatusProduct.NameStatus == "Hết hạn")
                         select p;
            }
            else
            {
                result = from p in DbConText.Products where p.NameProduct.IndexOf(KeyWord) > -1 &&
                         (p.StatusProduct.NameStatus == "Đã duyệt" || p.StatusProduct.NameStatus == "Hết hạn")
                         select p;
            }
            foreach (Product p in result)
                ListPro.Add(p);

            return View(ListPro);
        }

        public ActionResult Filter(int IDCatalogy)
        {
            string Selected = "1";
            try
            {
                Selected = System.Web.HttpContext.Current.Request.Cookies["ChooseCity"].Value;

            }
            catch (NullReferenceException e)
            {
                Selected = "1";
            }
            int IDProvince = int.Parse(Selected);
            List<Product> lstProduct = new List<Product>();
            dynamic result;
            if(IDProvince != -1)
            {
                result = from p in DbConText.Products where 
                             p.IDCatalogy == IDCatalogy && p.IDProvince == IDProvince &&
                             (p.StatusProduct.NameStatus == "Đã duyệt")  
                         select p;
            }
            else
            {
                result = from p in DbConText.Products where p.IDCatalogy == IDCatalogy &&
                         (p.StatusProduct.NameStatus == "Đã duyệt") 
                         select p;
            }
            
            foreach (var product in result)
            {
                lstProduct.Add(product);
            }

            return View(lstProduct);
        }

        public ActionResult Details(int idProduct)
        {
            Product product = DbConText.Products.SingleOrDefault(p => p.IDProduct == idProduct);
            if (product != null)
            {
                ViewBag.Images = product.Images.ToList();
                return View(product);
            }
            else return View();
        }

        //Shopping method
        [Authorize(Roles = "member")]
        public ActionResult ShoppingCart(int idProduct)
        {
            Product product = DbConText.Products.SingleOrDefault(p => p.IDProduct == idProduct);

            return View(product);
        }

        [HttpPost]
        public PartialViewResult PostComment(string textarea)
        {
            Account ac = DbConText.Accounts.SingleOrDefault(a => a.Email == User.Identity.Name);
            int idproduct = int.Parse(Request.Form["IDProduct"]);
            Comment CM = new Comment();
            CM.DateAdder = DateTime.Now;
            CM.Description = textarea;
            CM.IDAccount = ac.IDAccount;
            CM.IDProduct = idproduct;

            DbConText.AddToComments(CM);
            DbConText.SaveChanges();
            var result = from com in DbConText.Comments where com.IDProduct == idproduct select com;

            List<Comment> ListComment = result.ToList();
            ListComment.Reverse();
            return PartialView("_ListComment", ListComment);
        }
        public ActionResult DirectBuy(int idProduct)
        {

            Account account = DbConText.Accounts.SingleOrDefault(m => m.Email == User.Identity.Name);
            if (account != null)
            {

                Order order = new Order();
                order.IDProduct = idProduct;
                order.Total = decimal.Parse(Request.Form["total"]);
                order.IDStatus = 1;
                order.DateAdded = DateTime.Now;
                order.IDAccountReceive = account.IDAccount;
                order.NumberOfProduct = int.Parse(Request.Form["totalnumber"]);
                order.Voucher = false;
                order.PhoneNumberReceive = Request.Form["TextDienThoai"];
                order.AddressReceive = Request.Form["TextDiaChi"];
                DbConText.Orders.AddObject(order);

                //Update so luong product da ban
                Product product = DbConText.Products.SingleOrDefault(p => p.IDProduct == idProduct);
                {
                    product.NumberOfPurchased += (int)order.NumberOfProduct;
                }

                DbConText.SaveChanges();

                return RedirectToAction("OrderDetail");
            }

            return View();

        }



        public ActionResult About()
        {

            return View();
        }

        public ActionResult Subscribe(string email)
        {

            Email _email = Email.CreateNewEmail(email);
            if (_email != null)
            {
                _email.DateRegister = DateTime.Now;
                bool res = DbConText.AddEmail(_email);
                if (res)
                {
                    return Json("Gửi đăng kí thành công");
                }
                else
                {
                    return Json("Email đã đăng kí rồi");
                }
            }
            else
            {
                return Json("Email không hợp lệ");
            }
        }

        public String Send()
        {

            DbConText.SendSubscribeEmail();
            return "WTF";
        }

        public ActionResult Guide()
        {

            return View();
        }

        //View Order method
        public ActionResult OrderDetail()
        {
            Account account = DbConText.Accounts.SingleOrDefault(m => m.Email == User.Identity.Name);
            //  Order order = DbConText.Orders.SingleOrDefault(m => m.IDAccountReceive == account.IDAccount);
            
            var orderlist = from p in DbConText.Orders where p.IDAccountReceive == account.IDAccount select p;
            return View(orderlist.ToList());
        }

        
        public ActionResult SiteMaps()
        {
            return View();
        }


    }
}
